package ext.tianma.doc.forms;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import ext.generic.erp.common.CommonUtil;

/**
 * 读取XML文件中的节点获取节点中的值
 * @author KWang
 *
 */
public class RFQXMLUtil {
	private static String clazz = RFQXMLUtil.class.getName() ;
	
	private static final String CONFIG_FILE = "RFQ_Mapping.xml" ;
	
	private static final String RFQ_DOC_ATT_NAME = "RFQ-Doc" ;
	
	private static final String PART_FINISHED_GOOD_ATT_NAME = "Part-FinishedGood" ;
	
	private static final String ATTRIBUTE_ATT_NAME  = "attribute" ;
	
	private static final String DB_COLUMN_ATT_NAME = "dbColumn" ;
	
	private static final String IBA_NAME_ATT_NAME = "ibaName" ;
	
	private Map<String,String> rfqMapping = new HashMap<String,String>() ;
	
	private Map<String,String> finishedGoodMapping = new HashMap<String,String>() ;
	
	private static final RFQXMLUtil instance = new RFQXMLUtil() ;
	
	private RFQXMLUtil(){
		String filePath = RFQXMLUtil.class.getResource("").getPath() +  CONFIG_FILE ;
		
		parse( filePath ) ;
	}
	
	public static RFQXMLUtil getInstance(){
		return instance ;
	}

	public Map<String, String> getRfqMapping() {
		return rfqMapping;
	}

	public Map<String, String> getFinishedGoodMapping() {
		return finishedGoodMapping;
	}

	private void parse(String file){
		if( file == null ){
			return ;
		}
		
		try{
			//获取XML的DOM对象
			DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = fac.newDocumentBuilder();
			Document document = documentBuilder.parse(file);
			
			if(document == null){
				System.out.println(">>>>> Error Info : "+ clazz + ".parse(String file) : document == null ");
				
				return ;
			}else{
				initRFQMapping( document ) ;
				
				initFinishedGoodMapping( document ) ;
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			
		}
	}
	
	private void initRFQMapping( Document document ){
		this.rfqMapping = initMapping( document , RFQ_DOC_ATT_NAME ) ;
	}
	
	private void initFinishedGoodMapping( Document document ){		
		this.finishedGoodMapping = initMapping( document , PART_FINISHED_GOOD_ATT_NAME ) ;
	}
	
	private Map<String,String> initMapping(Document document , String tagName ){
		
		Map<String,String> map = null ;
		
		NodeList nodeList = document.getElementsByTagName( tagName );
		
		if( nodeList != null ){
			int size = nodeList.getLength() ;
			
			for(int i=0 ; i < size ; i++ ){
				Node node = nodeList.item(i) ;
				
				if( node != null && node instanceof Element){
					Element element = ( Element ) node ;
					
					map = initMapping(element );
				}
			}
		}
		
		if( map == null ){
			map = new HashMap<String,String>() ; 
		}
		
		return map ;
	}

	private Map<String,String> initMapping( Element element) {
		Map<String,String> map = new HashMap<String,String>() ;
		
		//获取子节点列表
		NodeList childList = element.getElementsByTagName( ATTRIBUTE_ATT_NAME ) ;
		
		if( childList != null ){
			int  childListSize = childList.getLength() ;
			
			for( int n = 0 ; n < childListSize ; n ++ ){
				Node childNode = childList.item(n) ;
				
				if( childNode != null && childNode instanceof Element ){
					Element childElement = ( Element ) childNode ;
					
					String dbColumn = getValue(childElement , DB_COLUMN_ATT_NAME );
					
					String ibaName = getValue(childElement , IBA_NAME_ATT_NAME );
					
					if( ( !dbColumn.equals("") ) && ( !ibaName.equals("") ) ){									
						map.put(dbColumn, ibaName) ;
					}
				}
			}
		}
		
		return map ;
	}

	private String getValue(Element childElement, String attName ) {
		String dbColumn = "" ;
		
		if(childElement == null){
			return dbColumn ;
		}
		
		if( attName == null || attName.trim().equals("") ){
			return dbColumn ;
		}
		
		dbColumn = childElement.getAttribute( attName ) ;
		if(dbColumn == null){
			dbColumn = "" ;
		}else{
			dbColumn = dbColumn.trim() ;
		}
		
		return dbColumn;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Map<String, String > rfqMap = RFQXMLUtil.getInstance().getRfqMapping() ;
		System.out.println("Debug   rfqMap="+rfqMap.size());
		
		CommonUtil.printMap( rfqMap ) ;
		
		Map<String, String > finishedGoodMap = RFQXMLUtil.getInstance().getFinishedGoodMapping() ;
		System.out.println("Debug   rfqMap="+finishedGoodMap.size());
		
		CommonUtil.printMap( finishedGoodMap ) ;

	}	
}
